STEP 5: This game is a competition with gravity! Let's add gravity and controls to work against the gravity.
An event sets up code that runs each time a signal is received. In this case, we'll use a signal from your keyboard.
From , drag out Set Gravity. Change 10 to the variable gravity. What value does it hold?
Go to the toolkit and drag out Space Bar. Change 5 in .jump() to the variable flappiness.
This event runs code every time you press the spacebar on your keyboard. Click Run and try it out!
To navigate the page using the TAB key, first press ESC to exit the code editor.
# Program variables (DON'T DELETE!!!)
block_speed = -6 # controls how fast the pipes move across the stage
block_gap = 200 # controls the space between the top and bottom of each pipe
block_interval = 1 # controls how often a new pipe appears
gravity = 10 # controls how fast the sprite falls
flappiness = 5 # controls how much the sprite moves up
# Store and display score
score = 0
score_text = codesters.Text("Score: " + str(score), 100, 200, "yellow")
########################################################################
# ADD CODE BELOW THIS LINE #
########################################################################
stage.set_background("space")
stage.disable_all_walls()
sprite = codesters.Sprite("dinosaur")
sprite.set_size(0.4)
sprite.set_say_color("yellow")
sprite.say("TAP THE SPACE BAR TO GUIDE ME THROUGH THE BLOCKS!", 3)
sprite.go_to(-200, 0)
t = codesters.Teacher()
set_gravitys = t.find_function("set_gravity")
defs = t.find_block("def")
jumps = t.find_function("jump")
try:
tval1 = set_gravitys[0][1].lower().replace(' ','')
except:
tval1 = "DNE"
try:
tval2 = defs[0][1].lower().replace(' ','')
except:
tval2 = "DNE"
try:
tval3 = jumps[0][1].lower().replace(' ','')
except:
tval3 = "DNE"
t1 = TestObjective()
t1.add_success('(gravity)' in tval1, "Great Job!")
t1.add_failure(tval1 == "DNE", "Did you add Set Gravity to the program?")
t1.add_failure(tval1 != "DNE" and '(gravity)' not in tval1, "Did you change the amount of gravity from 10 to the variable gravity?")
t2 = TestObjective()
t2.add_success('space_bar()' in tval2, "Great job!")
t2.add_failure(tval2 == "DNE", "Did you add a Space Bar event to the program?")
t2.add_failure(tval2 != "DNE" and 'space_bar()' not in tval2, "Are you sure you added the Space Bar event?")
t3 = TestObjective()
t3.add_success('(flappiness)' in tval3, "Great job!")
t3.add_failure(tval3 == "DNE", "Oops, did you delete the .jump() code from inside the event?")
t3.add_failure(tval3 != "DNE" and '(flappiness)' not in tval3, "Did you change the jump amount from 5 to the variable flappiness?")
tester = TestManager()
tester.add_test_list([t1, t2, t3])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.